package com.medical.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;

/**
 * @author TISNIW
 * @version 1.0
 * @date 2024/8/11 19:50
 * @desc
 */
@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
                .csrf(csrf -> csrf.disable())
                .sessionManagement(session -> session
                        .sessionCreationPolicy(SessionCreationPolicy.STATELESS))
                .authorizeHttpRequests(auth -> auth
                        .requestMatchers("/login/**", "/register/**","/**").permitAll()
                        .requestMatchers("/llm/**").authenticated() // 需要认证
                        .anyRequest().permitAll());

        return http.build();
    }

    /**
     * @author TISNIW
     * @version 1.0
     * @date 2024/7/1 16:30
     * @desc JWT相关配置
     */
    public static class JwtConstants {
        public static final String CLAIM_USER_ID = "userId"; // 用户ID声明名
        public static final String CLAIM_USERNAME = "username"; // 昵称声明名
        public static final String CLAIM_USER_KEY = "userKey";// 用户标识符
        public static final String SECRET = "aVeryLongSecretKeyThatIsExactlySixtyFourBytesLongForHS512aVeryLongSecretKeyThatIsExactlySixtyFourBytesLongForHS512"; // 安全密钥，请勿硬编码于生产环境中
        public static final long EXPIRATION = 604800000L; // 过期时间，默认为7天
        public static final String TOKEN_PREFIX = "Bearer "; // token前缀
        public static final String HEADER_STRING = "Authorization"; // 请求头中token的key
        public static final String TOKEN_REDIS_PREFIX = "token:key:"; // token在redis中的前缀
        public static final String SUBJECT = "JWT"; // 主题
        public static final String ISSUER = "hospital"; // 签发者
    }
}
